package com.java.IO.file_stream;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

public class FileOutputStreamAPI {
    public static void main(String[] args) throws IOException {
        String path = "File和IO流\\com\\java\\files\\byte_stream.txt";
        
        // 创建对象：若父级路径不存在则抛出异常FileNotFoundException，若文件不存在则会创建。若文件已存在则清空文件内数据，但设置参数2为true后则不会清空
        FileOutputStream fileOutputStream1 = new FileOutputStream(path);
        FileOutputStream fileOutputStream2 = new FileOutputStream(new File(path));
        FileOutputStream fileOutputStream3 = new FileOutputStream(new File(path), true);
        
        // write：写出数据到本地文件，若参数为整数则会转为ASCII对应的字符
        fileOutputStream1.write(97); // a
        fileOutputStream1.write('9'); // 9
        fileOutputStream1.write('中'); // -
        fileOutputStream1.write("\r\n".getBytes()); // 换行(Windows\r\n、Linux\n、Mac\r)
        fileOutputStream1.write(new byte[]{97, 98, 99, 100}); // abcd
        fileOutputStream1.write(new byte[]{97, 98, 99, 100, 101, 102, 103}, 3, 2); // de
        
        // close：关闭流并释放资源
        fileOutputStream1.close();
        fileOutputStream2.close();
        fileOutputStream3.close();
    }
}
